package com.example.demo.controller.user;

import com.example.demo.commom.AjaxResult;
import com.example.demo.commom.TokenUtil;
import com.example.demo.dto.user.class_info.ClassInfoDto;
import com.example.demo.dto.user.class_info.UpdateClassDto;
import com.example.demo.service.user.*;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/class")
public class ClassInfoController {
    @Autowired
    private ClassInfoService classInfoService;
    @Autowired
    private TeacherService teacherService;
    @Autowired
    private AdminService adminService;

    /**
     * 查询班级信息 - 只支持管理员和老师查看
     * @return
     */
    @GetMapping("/info")
    public AjaxResult info(HttpServletRequest request, Integer identity){
        // 参数校验
        String token = request.getHeader("token");
        Integer user_id = TokenUtil.getIdByToken(token);
        if(user_id == null || user_id <= 0 || user_id > 100000){
            return AjaxResult.fail(-1,"token不存在");
        }
        if(identity == 0){
            // 验证管理员的身份
            if(!adminService.authentication(user_id)){
                return AjaxResult.fail("管理员身份有误");
            }
        }
        if(identity == 1){
            // 验证教师的身份
            if(!teacherService.authentication(user_id)){
                return AjaxResult.fail("教师身份有误");
            }
        }
        if(identity == 0 ||identity == 1){
            return AjaxResult.success(classInfoService.getClassAll());
        }
        return AjaxResult.fail(-1, "参数错误");
    }
    /**
     * 修改学生到班级 - 只支持管理员和老师
     * @return
     */
    @PostMapping("/update/students")
    public AjaxResult update(HttpServletRequest request, @RequestBody UpdateClassDto updateClassDto) {
        // 参数校验
        String token = request.getHeader("token");
        Integer user_id = TokenUtil.getIdByToken(token);
        if(user_id == null || user_id <= 0 || user_id > 100000){
            return AjaxResult.fail(-1,"token不存在");
        }
        if(updateClassDto.getIdentity() == 0){
            // 验证管理员的身份
            if(!adminService.authentication(user_id)){
                return AjaxResult.fail("管理员身份有误");
            }
        }
        if(updateClassDto.getIdentity() == 1){
            // 验证教师的身份
            if(!teacherService.authentication(user_id)){
                return AjaxResult.fail("教师身份有误");
            }
        }
        if(updateClassDto.getIdentity() == 0 ||updateClassDto.getIdentity() == 1){
            return AjaxResult.success(classInfoService.updateClass(updateClassDto.getStudent_ids(), updateClassDto.getClass_id()));
        }
        return AjaxResult.fail(-1, "参数错误");
    }

    /**
     * 新增班级 - 只支持管理员和老师
     * @return
     */
    @PostMapping("/add")
    public AjaxResult add(HttpServletRequest request, @RequestBody ClassInfoDto classInfoDto) {
        // 参数校验
        String token = request.getHeader("token");
        Integer user_id = TokenUtil.getIdByToken(token);
        if(user_id == null || user_id <= 0 || user_id > 100000){
            return AjaxResult.fail(-1,"token不存在");
        }
        if(classInfoDto.getIdentity() == 0){
            // 验证管理员的身份
            if(!adminService.authentication(user_id)){
                return AjaxResult.fail("管理员身份有误");
            }
        }
        if(classInfoDto.getIdentity() == 1){
            // 验证教师的身份
            if(!teacherService.authentication(user_id)){
                return AjaxResult.fail("教师身份有误");
            }
        }
        if(classInfoDto.getIdentity() == 0 ||classInfoDto.getIdentity() == 1){
            return AjaxResult.success(classInfoService.add(classInfoDto));
        }
        return AjaxResult.fail(-1, "参数错误");
    }
}
